/**
 * Created by shidaiapp on 2017/9/29.
 */
import {formatDate} from "../../../js/dateUtils"
import ApiBase from "../../../../api/moudles/ApiBase"
import tiyData from "./image_data";

function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalFolder.isLoading = false;
  tiyData.tStatus.modalDelete.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalFolder.isLoading = true;
    tiyData.tStatus.modalDelete.isLoading = true;
  });

}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  //resetForm(self, tiyData);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.folderName = '';
  tiyData.tParams.orderBy = '';
  tiyData.tStatus.breadcrumbList = [{
    id: "",
    folderType: 0,
    level: 0,
    name: 'Home'
  }];
  tiyData.tParams.folderList.level=1;
  tiyData.tParams.folderList.foderId = null;
  tiyData.tParams.folderList.folderName = null;
  tiyData.tParams.folderList.orderList = null;
};


/**
 * 新件文件夹
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webAdd = (self, tiyData, name) => {
  tiyData.tStatus.modalFolder.isLoading = true;
  self.$refs[name].validate((valid) => {
    if (valid) {
      self.$apiImage.createFolder({
        folderName: tiyData.tParams.folder.name,
        folderId: tiyData.tParams.folder.parentId,//父id
      }).then(function (response) {
        changeModelLoading(self, tiyData);
        if (response.success) {
          tiyData.tStatus.modalFolder.isShow = false;
          webFolderListByParentId(self, tiyData, null);
        } else {
          self.$Message.error(response.errorMessage);
        }
      });
    } else {
      changeModelLoading(self, tiyData);
    }
  });
};
/**
 * 删除对象
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webDelete = (self, tiyData) => {
  self.$Spin.show();
  tiyData.tStatus.modalDelete.isLoading = true;
  let ids = '';
  tiyData.tStatus.modalDelete.folder.objectIds.forEach(function (item) {
    ids = ids + item + ',';
  });
  self.$apiImage.deleteFolderOrImages({
    objectIds: ids,
    type: tiyData.tStatus.modalDelete.folder.type
  }).then(function (response) {
    self.$Spin.hide();
    tiyData.tStatus.batchDelete.isBatch=false;
    changeModelLoading(self, tiyData);
    if (response.success) {
      tiyData.tStatus.modalDelete.isShow = false;
      webFolderListByParentId(self, tiyData, null);
    } else {
      self.$Message.error(response.errorMessage);
    }

  });
};


/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiImage.getFolderList({

  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.folderTreeData.splice(0);
      tiyData.tData.folderList.splice(0);
      let folderPage = response.result;
      tiyData.tPage.total = folderPage.total;
      tiyData.tPage.stepNumber = folderPage.pageSize;
      folderPage.list.forEach(
        function (item) {
          let treeDataItem = createTreeDataItem(self, item);
          tiyData.tData.folderList.push(item);
          tiyData.tData.folderTreeData.push(treeDataItem);
        }
      );
      if (0 == tiyData.tData.folderTreeData.length) {
        self.$Message.error("查询没有结果");
      }
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webFolderListByParentId = (self, tiyData, folderItem) => {
  self.$Spin.show();
  tiyData.tStatus.batchDelete.isBatch = false;
  if(folderItem!=null){
    breadcrumbData(self, tiyData, folderItem);
  }else{
    tiyData.tParams.folder.parentId = "";
    tiyData.tParams.uploadParam.folderId = "";
    let requestMsg = ApiBase.createRequestMsg(tiyData.tParams.uploadParam);
    tiyData.tParams.uploadFolderParam = {requestMsg: JSON.stringify(requestMsg)}
  }
  self.$apiImage.getFolderListByParentId({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "foderId": tiyData.tParams.folderList.foderId,
    "keyCode": tiyData.tParams.folderList.keyCode,
    "folderType": tiyData.tParams.folderList.folderType,
    "productCode": tiyData.tParams.folderList.productCode,
    "folderName": tiyData.tParams.folderList.folderName,
    "orderList": tiyData.tParams.folderList.orderList,
    "level": tiyData.tParams.folderList.level,
  }).then(function (response) {
    self.$Spin.hide();
    tiyData.tData.folderList.splice(0);
    if(tiyData.tParams.folderList.level==1){
      tiyData.tData.folderTreeData.splice(0);
    }
    if (response.success) {
      //刷新一下数据 否则选择图片的div不会出现
      tiyData.tStatus.refreshChecked = !tiyData.tStatus.refreshChecked;
      let folderTreeChilder = [];
      let folderPage = response.result;
      tiyData.tPage.total = folderPage.total;
      tiyData.tPage.stepNumber = folderPage.pageSize;
      folderPage.list.forEach(
        function (item) {
          if ('' == item.suffix) {
            let treeDataItem = createTreeDataItem(self, item);
            folderTreeChilder.push(treeDataItem);
            if(tiyData.tParams.folderList.level==1){
              tiyData.tData.folderTreeData.push(treeDataItem);
            }
          }
          tiyData.tData.folderList.push(item);
        }
      );
      treeData(self, tiyData, folderTreeChilder);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

//创建树数据的对象
function createTreeDataItem(self, item) {
  return {
    title: item.name,
    name: item.name,
    expand: false,
    id: item.id,
    level: item.level,
    parentId: item.parentId,
    parentName: item.parentName,
    children:[],
    render: (h, {root, node, data}) => {
      // return <span style=" display: 'inline-block';width: '100%'"><span><Icon type="ios-folder-outline"></Icon>data.title</span></span>
      return h('span', {
        style: {
          display: 'inline-block',
          float: 'left',
          marginLeft: '5px'
        }, on: {
          click: () => {
            self.selectedForTree(data);
          }
        },
      }, [
        h('span',
          [
            h('Icon', {
              props: {
                type: data.expand ? 'ios-folder' : 'ios-folder-outline',
                size: 13
              },
              style: {
                marginRight: '8px'
              }
            }),
            h('span', data.title)
          ]),
      ]);
    },
  };
}

//对面包屑导航栏的处理
function breadcrumbData(self, tiyData, folderItem) {
  if (null != folderItem) {
    tiyData.tParams.uploadParam.folderId = folderItem.id;
    let requestMsg = ApiBase.createRequestMsg(tiyData.tParams.uploadParam);
    tiyData.tParams.folder.parentId = folderItem.id;
    tiyData.tParams.uploadFolderParam = {requestMsg: JSON.stringify(requestMsg)}
    //文件级别导航栏
    if (1 === folderItem.level) {//level 从1开始 假如点击的是第一级的 初化化breadcrumb
      tiyData.tStatus.breadcrumbList.splice(0);
      tiyData.tStatus.breadcrumbList.push({
        id: "",
        folderType: 0,
        level: 0,
        name: 'Home'
      });
      tiyData.tStatus.breadcrumbList.push(folderItem);
    } else {
      //导航栏最大的level
      let barMaxLevel = tiyData.tStatus.breadcrumbList[tiyData.tStatus.breadcrumbList.length - 1].level;
      if (folderItem.level > barMaxLevel) {
        tiyData.tStatus.breadcrumbList.push(folderItem);
      } else {
        tiyData.tStatus.breadcrumbList.forEach(function (item, index) {
          if (item.level == folderItem.level) {
            tiyData.tStatus.breadcrumbList.splice(index, tiyData.tStatus.breadcrumbList.length - index, folderItem);
          }
        });
      }
    }
  }
}

//对左侧树形控件进行操作
function treeData(self, tiyData, folderTreeChilder) {
  let nowTreeData;
  tiyData.tData.folderTreeData.forEach(function (itemTree) {
    itemTree.expand = false;
  });
  //面包屑数据的级别已经定型  从面包屑数据开始进行对比
  tiyData.tStatus.breadcrumbList.filter(breadcrum => breadcrum.name != 'Home').forEach(function (item, index) {
    if (index == 0) {
      nowTreeData = tiyData.tData.folderTreeData.filter(folderTree => folderTree.level == item.level && folderTree.id == item.id)[0];
      tiyData.tData.folderTreeData.forEach(function (itemTree) {
        if (itemTree.level == item.level && itemTree.id != item.id) {
          itemTree.children = [];
          itemTree.expand = false;
        } else {
          itemTree.expand = true;
        }
      });
    } else {
      if(nowTreeData.children){
        nowTreeData.children.forEach(function (itemTree) {
          if (itemTree.level === item.level && itemTree.id !== item.id) {
            itemTree.children = [];
            itemTree.expand = false;
          } else {
            itemTree.expand = true;
          }
        });
        nowTreeData = nowTreeData.children.filter(folderTree => folderTree.level === item.level && folderTree.id === item.id)[0];
      }

    }
  });
  if(nowTreeData!==undefined){
    nowTreeData.children = folderTreeChilder;
    nowTreeData.expand = true;
  }

}

function ShowTheObject(obj, tag) {
  if (null == tag) {
    tag = '';
  }
  var des = "";
  for (var name in obj) {
    des += name + ":" + obj[name] + ";";
  }
}

